<?php
namespace UHU;
interface WebComic
{
	public function title();
	public function description();
	public function link();
	public function image();
	public function copyright();
	public function language();
	public function category();
}
namespace UHU\WebComic;
interface Issue
{
	public function title();
	public function description();
	public function link();
}
interface Dated_Issue
{
	public function pubDate();
}
interface funcs
{
	public static function doc(RSS $webcomic);
}
class RSS extends \UHU\Collection\Class_Static implements \UHU\WebComic, funcs
{
	public static function getReflector()
	{
		static $reflector;
		if(isset($reflector) === false)
		{
			$reflector = new \ReflectionClass('\UHU\WebComic\Issue');
		}
		return $reflector;
	}
	protected $title;
	protected $description;
	protected $link;
	protected $image;
	protected $copyright;
	public function __construct($title,$description,$link,$image,$copyright)
	{
		$this->title       = $title;
		$this->description = $description;
		$this->link        = $link;
		$this->image       = $image;
		$this->copyright   = $copyright;
	}
	public function title()
	{
		return $this->title;
	}
	public function description()
	{
		return $this->description;
	}
	public function link()
	{
		return $this->link;
	}
	public function image()
	{
		return $this->image;
	}
	public function copyright()
	{
		return $this->copyright;
	}
	public function language()
	{
		return 'en';
	}
	public function category()
	{
		return 'webcomic';
	}
	public static function doc(RSS $webcomic)
	{
		$title = htmlentities($webcomic->title(),ENT_NOQUOTES,'UTF-8',false);
		$description = htmlentities($webcomic->description(),ENT_NOQUOTES,'UTF-8',false);
		$link = htmlentities($webcomic->link(),ENT_NOQUOTES,'UTF-8',false);
		$doc =
			'<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">' .
			"\n" . '<channel>' .
			"\n\t" . '<atom:link rel="self" type="application/rss+xml" href="http://services.signpostmarv.name/webcomic-rss/' . rawurlencode($webcomic->title()) . '.rss" />' .
			"\n\t" . '<title>' . $title . '</title>' .
			"\n\t" . '<description>' . $description . '</description>' .
			"\n\t" . '<link>' . $link . '</link>' .
			"\n\t" . '<generator>http://signpostmarv.name/webcomic-rss/</generator>' .
			"\n\t" . '<language>' . htmlentities($webcomic->language(),ENT_NOQUOTES,'UTF-8',false) . '</language>' .
			"\n\t" . '<category>' . htmlentities($webcomic->category(),ENT_NOQUOTES,'UTF-8',false) . '</category>'
		;
		if(is_string($webcomic->copyright()) && strlen(trim($webcomic->copyright())) > 0)
		{
			$doc .=
				"\n\t" . '<copyright>' . htmlentities($webcomic->copyright(),ENT_NOQUOTES,'UTF-8',false) . '</copyright>'
			;
		}
		if(is_string($webcomic->image()) && strlen(trim($webcomic->image())) > 0)
		{
			$doc .=
				"\n\t" . '<image>' .
				"\n\t\t" . '<title>' . $title . '</title>' .
				"\n\t\t" . '<description>' . $description . '</description>' .
				"\n\t\t" . '<url>' . htmlspecialchars($webcomic->image(),ENT_NOQUOTES,'UTF-8',false) . '</url>' .
				"\n\t\t" . '<link>' . $link . '</link>' .
				"\n\t" . '</image>'
			;
		}
		foreach($webcomic as $item)
		{
			$doc .=
				"\n\t" . '<item>' .
				"\n\t\t" . '<title>' . htmlentities($item->title(),ENT_NOQUOTES,'UTF-8',false) . '</title>' .
				"\n\t\t" . '<guid isPermaLink="true">' . htmlspecialchars($item->link(),ENT_NOQUOTES,'UTF-8',false) . '</guid>' .
				"\n\t\t" . '<description>' . htmlentities($item->description(),ENT_NOQUOTES,'UTF-8',false) . '</description>'
			;
			if($item instanceof Dated_Issue)
			{
				$doc .= "\n\t\t" . '<pubDate>' . htmlentities($item->pubDate(),ENT_NOQUOTES,'UTF-8',false) . '</pubDate>';
			}
			$doc .= "\n\t" . '</item>';
		}
		$doc .=
			"\n" . '</channel>' .
			"\n" . '</rss>'
		;
		return $doc;
	}
}

class Parser
{
	const max_items = 50;
	protected $title;
	protected $description;
	protected $link;
	protected $image;
	protected $copyright;
	protected $source_url;
	protected $sprintf_link;
	protected $RSS;
	public function __construct($title,$description,$link,$image,$copyright,$source_url,$sprintf_link)
	{
		$this->title        = $title;
		$this->description  = $description;
		$this->link         = $link;
		$this->image        = $image;
		$this->copyright    = $copyright;
		$this->source_url   = $source_url;
		$this->sprintf_link = $sprintf_link;
		$this->RSS = new RSS($title,$description,$link,$image,$copyright);
		$this->nab();
	}
	public function RSS()
	{
		return $this->RSS;
	}
	protected function nab()
	{
		if(($ch = curl_init($this->source_url)) === false)
		{
			throw new \RuntimeException('Could not initialise cURL.');
		}
		curl_setopt($ch,CURLOPT_HEADER,false);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		if(($doc = curl_exec($ch)) === false)
		{
			throw new \RuntimeException('Execution of cURL failed.');
		}
		curl_close($ch);
		return $doc;
	}
}

require_once('WebComic/RSS.php');
require_once('WebComic/Parser.php');
require_once('WebComic/Parser/RegEx.php');
?>